
package Graphics;

import Settings.*;
import org.lwjgl.*;
import org.lwjgl.opengl.*;
import org.lwjgl.util.glu.*;

/**
 *
 * @author RikuXan
 */
public class Displays
{
    private DisplayMode toUse;

    public Displays()
    {
        try
        {
            init();
            //initOpenGL();
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
    }
    
    

    public void init() throws LWJGLException
    {
        //getDMSettings();
        setToUse(  Settings.wWidth ,  Settings.wHeight , Settings.wFrequency );
        Display.setDisplayMode( toUse ); // TODO create/search by settings
        Display.setTitle( "SnakeOnline" );
        Display.setInitialBackground( 1 , 1 , 1 );
        Display.setFullscreen( Settings.wFullscreen );
        Display.create();
        initOpenGL();
    }

    public void setToUse( int width , int height , int frequency )
    {
        try
        {
            DisplayMode[] allModes = Display.getAvailableDisplayModes();

            for( int i = 0 ; i < allModes.length ; i++ )
            {
                if( ( allModes[i].getWidth() == width ) && ( allModes[i].getHeight() == height ) && ( allModes[i].getFrequency() == frequency ) )
                {
                    toUse = allModes[i];
                    return;
                }
                //System.out.println( "Width: " + allModes[i].getWidth() + " - Height: " + allModes[i].getHeight() + " - Frequency: " + allModes[i].getFrequency() );
            }
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }

        Display.setVSyncEnabled( Settings.wVSync );

        if( toUse == null )
        {
            toUse = new DisplayMode( width , height ); // TODO desired resolution
        }
    }

    public void initOpenGL()
    {
        GL11.glEnable(GL11.GL_TEXTURE_2D);

        // 2D -> kein depthtest
        GL11.glDisable(GL11.GL_DEPTH_TEST);

        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();

        GL11.glOrtho(0, Settings.wWidth, Settings.wHeight, 0, -1, 1);

    }

    public boolean isCloseRequested()
    {
        return Display.isCloseRequested();
    }

    public void destroy()
    {
        Display.destroy();
    }

    public void update()
    {
        Display.update();
    }

    public void makeCurrent()
    {
        try
        {
            Display.makeCurrent();
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
    }

    public boolean isActive()
    {
        return Display.isActive();
    }

    public boolean isVisible()
    {
        return Display.isVisible();
    }

    public boolean isDirty()
    {
        return Display.isDirty();
    }

    public void sync( int frames )
    {
        Display.sync( frames );
    }

    public void setFullscreen( boolean fs )
    {
        try
        {
            if( !toUse.isFullscreenCapable() )
            {
                Sys.alert( "Error" , "Fullscreen not possible!" );
                return;
            }

            Display.setFullscreen( fs );
        }
        catch( Exception e )
        {
            Sys.alert( "Error" , "Fullscreen not possible!" );
            try
            {
                Display.setFullscreen( false );
            }
            catch( Exception s )
            {
                e.printStackTrace();
            }
            e.printStackTrace();
        }
    }
}
